#include "Protection.h"

#include "Talent.h"
#include "TalentStatIncrease.h"
#include "Warrior.h"
#include "WarriorSpells.h"

Protection::Protection(Warrior* pchar) :
    TalentTree("Protection", "Assets/warrior/warrior_protection.jpg"), warrior(pchar), spells(static_cast<WarriorSpells*>(pchar->get_spells())) {
    talent_names_to_locations = {
        {"Shield Specialization", "1ML"},
        {"Anticipation", "1MR"},
        {"Improved Bloodrage", "2LL"},
        {"Toughness", "2MR"},
        {"Iron Will", "2RR"},
        {"Last Stand", "3LL"},
        {"Improved Revenge", "3MR"},
        {"Defiance", "3RR"},
        {"Improved Sunder Armor", "4LL"},
        {"Improved Disarm", "4ML"},
        {"Improved Taunt", "4MR"},
        {"Improved Shield Wall", "5LL"},
        {"Concussion Blow", "5ML"},
        {"Improved Shield Bash", "5MR"},
        {"One-Handed Weapon Specialization", "6MR"},
        {"Shield Slam", "7ML"},
    };

    QMap<QString, Talent*>
        tier1 {{"1ML",
                new Talent(pchar, this, "Shield Specialization", "1ML", base_url + "items/Inv_shield_06.png", 5,
                           "Increases your chance to block attacks with a shield by %1% and has a %2% chance to generate 1 rage when a block occurs.",
                           QVector<QPair<unsigned, unsigned>> {{1, 1}, {20, 20}})},
               {"1MR", new Talent(pchar, this, "Anticipation", "1MR", base_url + "spell/Spell_nature_mirrorimage.png", 5,
                                  "Increases your Defense skill by %1.", QVector<QPair<unsigned, unsigned>> {{2, 2}})}};
    add_talents(tier1);

    QMap<QString, Talent*> tier2 {{"2LL", new Talent(pchar, this, "Improved Bloodrage", "2LL", base_url + "ability/Ability_racial_bloodrage.png", 2,
                                                     "Increases the instant Rage generated by your Bloodrage ability by %1.",
                                                     QVector<QPair<unsigned, unsigned>> {{2, 3}})},
                                  {"2MR", new TalentStatIncrease(pchar, this, "Toughness", "2MR", base_url + "spell/Spell_holy_devotion.png", 5,
                                                     "Increases your armor value from items by %1%.", QVector<QPair<unsigned, unsigned>> {{2, 2}},
                                                     QVector<QPair<TalentStat, unsigned>> {{TalentStat::ArmorModFromItems, 2}})},
                                  {"2RR", new Talent(pchar, this, "Iron Will", "2RR", base_url + "spell/Spell_magic_magearmor.png", 5,
                                                     "Increases your chance to resist Stun and Charm effects by an additional %1%.",
                                                     QVector<QPair<unsigned, unsigned>> {{3, 3}})}};
    add_talents(tier2);

    QMap<QString, Talent*> tier3 {{"3LL", new Talent(pchar, this, "Last Stand", "3LL", base_url + "spell/Spell_holy_ashestoashes.png", 1,
                                                     "When activated, this ability temporarily grants you 30% of your maximum hit points for 20 "
                                                     "seconds. After the effect expires, the hit points are lost.",
                                                     QVector<QPair<unsigned, unsigned>>())},
                                  {"3MR", new Talent(pchar, this, "Improved Revenge", "3MR", base_url + "ability/Ability_warrior_revenge.png", 3,
                                                     "Gives your Revenge ability a %1% chance to stun the target for 3 sec.",
                                                     QVector<QPair<unsigned, unsigned>> {{15, 15}})}};

    add_defiance(tier3);
    add_improved_shield_block(tier3);
    add_talents(tier3);

    QMap<QString, Talent*> tier4 {{"4ML", new Talent(pchar, this, "Improved Disarm", "4ML", base_url + "ability/Ability_warrior_disarm.png", 3,
                                                     "Increases the duration of your Disarm ability by %1 secs.",
                                                     QVector<QPair<unsigned, unsigned>> {{1, 1}})},
                                  {"4MR", new Talent(pchar, this, "Improved Taunt", "4MR", base_url + "spell/Spell_nature_reincarnation.png", 2,
                                                     "Reduces the cooldown of your Taunt ability by %1 secs.",
                                                     QVector<QPair<unsigned, unsigned>> {{1, 1}})}};

    add_improved_sunder_armor(tier4);
    add_talents(tier4);

    QMap<QString, Talent*> tier5 {{"5LL", new Talent(pchar, this, "Improved Shield Wall", "5LL", base_url + "ability/Ability_warrior_shieldwall.png",
                                                     2, "Increases the effect duration of your Shield Wall ability by %1 secs.",
                                                     QVector<QPair<unsigned, unsigned>> {{3, 2}})},
                                  {"5ML", new Talent(pchar, this, "Concussion Blow", "5ML", base_url + "ability/Ability_thunderbolt.png", 1,
                                                     "Stuns the opponent for 5 sec.", QVector<QPair<unsigned, unsigned>>())},
                                  {"5MR", new Talent(pchar, this, "Improved Shield Bash", "5MR", base_url + "ability/Ability_warrior_shieldbash.png",
                                                     2, "Gives your Shield Bash ability a %1% chance to silence the target for 3 sec.",
                                                     QVector<QPair<unsigned, unsigned>> {{50, 50}})}};
    add_talents(tier5);

    QMap<QString, Talent*> tier6 {
        {"6MR", new Talent(pchar, this, "One-Handed Weapon Specialization", "6MR", base_url + "items/Inv_sword_20.png", 5,
                           "Increases the damage you deal with One-Handed Melee weapons by %1%.", QVector<QPair<unsigned, unsigned>> {{2, 2}})}};
    add_talents(tier6);

    QMap<QString, Talent*> tier7 {};
    add_shield_slam(tier7);
    add_talents(tier7);

    talents["1ML"]->talent->set_bottom_child(talents["3ML"]->talent);
    talents["3ML"]->talent->set_parent(talents["1ML"]->talent);

    talents["2LL"]->talent->set_bottom_child(talents["3LL"]->talent);
    talents["3LL"]->talent->set_parent(talents["2LL"]->talent);

    talents["5ML"]->talent->set_bottom_child(talents["7ML"]->talent);
    talents["7ML"]->talent->set_parent(talents["5ML"]->talent);
}

void Protection::add_shield_slam(QMap<QString, Talent*>& talent_tier) {
    auto talent = get_new_talent(warrior, "Shield Slam", "7ML", base_url + "items/Inv_shield_05.png", 1,
                                 "Slam the target with your shield, causing 225 to 236 damage, modified by your shield block value, and has a 50%"
                                 "chance of dispelling 1 magic effect on the target. Also causes a high amount of threat.",
                                 QVector<QPair<unsigned, unsigned>>(),
                                 QVector<SpellRankGroup*> {spells->get_spell_rank_group_by_name("Shield Slam")});

    add_talent_to_tier(talent_tier, talent);
}

void Protection::add_improved_shield_block(QMap<QString, Talent*>& talent_tier) {
    QMap<unsigned, QString> rank_descriptions;
    QString base_str = "Allows your Shield Block ability to block an additional attack and increases the duration by %1 seconds.";
    rank_descriptions.insert(0, base_str.arg(0.5));
    rank_descriptions.insert(1, base_str.arg(0.5));
    rank_descriptions.insert(2, base_str.arg(1));
    rank_descriptions.insert(3, base_str.arg(2));
    Talent* talent = new Talent(warrior, this, "Improved Shield Block", "3ML", base_url + "ability/Ability_defend.png", 3, rank_descriptions, {});

    add_talent_to_tier(talent_tier, talent);
}

void Protection::add_improved_sunder_armor(QMap<QString, Talent*>& talent_tier) {
    auto talent = get_new_talent(warrior, "Improved Sunder Armor", "4LL", base_url + "ability/Ability_warrior_sunder.png", 3,
                                 "Reduces the cost of your Sunder Armor ability by %1 rage.", QVector<QPair<unsigned, unsigned>> {{1, 1}},
                                 QVector<SpellRankGroup*> {spells->get_spell_rank_group_by_name("Sunder Armor")});

    add_talent_to_tier(talent_tier, talent);
}

void Protection::add_defiance(QMap<QString, Talent*>& talent_tier) {
    auto talent = get_new_talent(warrior, "Defiance", "3RR", base_url + "ability/Ability_warrior_innerrage.png", 5,
                                 "Increase the threat generated by your attacks by %1% while in Defensive Stance.",
                                 QVector<QPair<unsigned, unsigned>> {{3, 3}}, QVector<SpellRankGroup*> {},
                                 QVector<Buff*> {spells->get_defensive_stance_buff()});

    add_talent_to_tier(talent_tier, talent);
}
